<template>
	<section>
	<el-dialog class="Blue" :title="$t('map.menuDeviceAdd')" width="50%" :visible="this.$store.state.user.visibles.deviceAddForm" :before-close="handleClose" :lock-scroll="false">
		<el-form label-width="80px" :model="addForm" ref="addForm">
			<el-row :gutter="20">
			<el-col :span="12">
			<el-form-item :label="$t('common.deviceType')" prop="typeId">
				 <el-select v-model="addForm.typeId"  placeholder="设备类型">
						<el-option
							v-for="item in this.$store.state.user.dict.deviceTypes"
							:key="item.id"
							:label="item.name"
							:value="item.id">
						</el-option>
					</el-select>
			</el-form-item>
			<el-form-item :label="$t('common.deviceImei')" prop="imei" :rules="[{ required: true, message: '请输入设备号', trigger: 'blur'},{ min: 10, max: 16, message: '长度在 10 到 16 个字符', trigger: 'blur'}]">
				<el-input type="text" v-model="addForm.imei" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item :label="$t('common.deviceName')" prop="name">
				<el-input type="text" v-model="addForm.name" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item :label="$t('common.deviceSim')" prop="sim">
				<el-input type="text" v-model="addForm.sim" auto-complete="off"></el-input>
			</el-form-item>
			</el-col>
			<el-col :span="12" v-if="hasPermission('user:group:view')">
			 <GroupTree @handleNodeClick="handleNodeClick" :currentKey="currentKey"></GroupTree>
		 </el-col>
	 </el-row>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button @click.native="handleClose">{{$t('common.cancel')}}</el-button>
			<el-button type="primary" @click.native="addSubmit">{{$t('common.ok')}}</el-button>
		</div>
	</el-dialog>


	<el-dialog  :title="$t('map.menuDeviceEdit')" width="30%"  :visible.sync="$store.state.user.visibles.deviceEditForm" :lock-scroll="false">
		<el-form label-width="80px" :model="editForm"  ref="editForm" >

				<el-form-item :label="$t('common.deviceImei')" prop="imei">
					<el-input type="text" v-model="editForm.imei" auto-complete="off" disabled></el-input>
				</el-form-item>
				<el-form-item :label="$t('common.deviceName')" prop="name">
					<el-input type="text" v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item :label="$t('common.deviceSim')" prop="sim">
					<el-input type="text" v-model="editForm.sim" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item :label="$t('common.deviceType')" prop="typeId">
					 <el-select v-model="typeId"  placeholder="设备类型">
							<el-option
								v-for="item in this.$store.state.user.dict.deviceTypes"
								:key="item.id"
								:label="item.name"
								:value="item.id">
							</el-option>
						</el-select>
				</el-form-item>
				<el-form-item label="限速" prop="speedLimit">
					<el-input-number v-model="speedLimit" :min="0" :max="200" label="描述文字"></el-input-number>
					<el-tag v-if="speedLimit==0" type="info">不限速</el-tag>
					<el-tag v-else  type="warning">限速 {{speedLimit}}km/h</el-tag>
				</el-form-item>

		</el-form>


		<div slot="footer" class="dialog-footer">
			<el-button @click.native="handleClose">{{$t('common.cancel')}}</el-button>
			<el-button type="primary" @click.native="editSubmit">{{$t('common.ok')}}</el-button>
		</div>
	</el-dialog>
</section>
</template>

<script>
	import GroupTree from './GroupTree'; //设备新增
	import { deviceAdd,updateDevice } from '@/api/api'

	export default {
		name: 'device',
		components: {
			GroupTree
		},
		data() {
			return {
				num:0,
				addForm: {
					imei: '',
					name: '',
					sim: '',
					typeId:1,
					groupId:0
				},
				typeId:1,
				speedLimit:0,
				currentKey:null
			}
		},
		methods: {
			handleClose: function() {
				this.$store.dispatch('setVisblesFlag', [{key:'deviceEditForm',value:false},{key:'deviceAddForm',value:false}]);
			},
			addSubmit: function() {
				this.$refs.addForm.validate((valid) => {
					if(valid) {
						var params = {
							imei:this.addForm.imei,
							name: this.addForm.name,
							sim: this.addForm.sim,
							typeId:this.addForm.typeId,
							groupId:this.addForm.groupId
						};
						deviceAdd(params).then((data) => {
						  this.$emit('add-success')
//								params.id = data;
//								this.$store.dispatch('addDevice', params);
//								this.$store.dispatch('setVisblesFlag', [{key:'deviceAddForm',value:false}]);
//								this.$refs.addForm.resetFields();
//								this.$store.dispatch('INIT_DEVICES_DATA'); //初始化设备数据
						});
					}
				});
			},
			editSubmit(){
				this.$refs.editForm.validate((valid) => {
					if(valid) {
						this.editForm.speedLimit = this.speedLimit;
						this.editForm.typeId = this.typeId;
						updateDevice(this.editForm).then((res) => {
							this.$store.dispatch('setVisblesFlag', [{key:'deviceEditForm',value:false}]);
							this.$store.dispatch('INIT_DEVICES_DATA');
							});
					 }
				});
			},
			handleNodeClick(node){
				this.addForm.groupId = node.id;
				this.editForm.groupId = node.id

			}
		},
		mounted() {

		},
		computed: {
			editForm:function(){
				let editDevice ={
				}
				if(this.$store.state.user.devices.selectId!=0){
					let index = this.$store.state.user.devices.index[this.$store.state.user.devices.selectId];
					let device  =  this.$store.state.user.devices.list[index];
					 editDevice = {
								id:device.id,
								name:device.name,
								imei:device.imei,
								sim:device.sim,
								typeId:device.typeId,
								groupId:device.groupId|0,
								speedLimit:device.speedLimit|0

					}
					this.typeId = device.typeId
					this.speedLimit = device.speedLimit|0
				}
				return editDevice;
			}
		},
		watch: {
		 '$store.state.user.visibles.deviceEditForm': function() {
				 if(this.$store.state.user.visibles.deviceEditForm){
					 let index = this.$store.state.user.devices.index[this.$store.state.user.devices.selectId];
	 				 let device  =  this.$store.state.user.devices.list[index];
					 this.currentKey = device.groupId;
				 }
			 }
		 },
	}
</script>
